/*
 * Copyright 2011 Dominik Pretzsch <dominik.pretzsch at gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.blacksheep.jmoteextract.component;


import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;


/**
 * <code>ProgressRenderer</code> provides a <code>TableCellRenderer</code> to show <br>
 * a progress-bar in a grid's cell. 
 * 
 * @author Dominik Pretzsch <dominik.pretzsch at gmail.com>
 * @TODO: rewrite
 */
public class ProgressRenderer extends DefaultTableCellRenderer {

    // @TODO: I18N
    private final String STR_CANCELED = "Canceled";
    private final String STR_DONE     = "Done!";
    private final String STR_PAUSED   = "Paused";
    private final String STR_TRYCON   = "Connecting...";
    private final String STR_DISCON   = "Connection lost";
    private final String STR_ERROR    = "Error during extraction :-(";
    private final String STR_UNKNOWN  = "State unknown";
    
    public static final int INT_PAUSED   = -1;
    public static final int INT_TRYCON   = -2;
    public static final int INT_DISCON   = -3;
    public static final int INT_ERROR    = -4;
    public static final int INT_CANCELED = -5;
    
    private final JProgressBar b = new JProgressBar(0, 100);

    public ProgressRenderer() {
        super();
        b.setStringPainted(true);
        b.setOpaque(true);
        b.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, 
                                                   Object value,
                                                   boolean isSelected, 
                                                   boolean hasFocus, 
                                                   int row, 
                                                   int column) {
                                                       
        Integer i = (Integer) value;
        String text = STR_DONE;
        
        if (i < 0) {
            switch (i) {
                case INT_PAUSED:
                    text = STR_PAUSED;
                    break;
                case INT_TRYCON:
                    text = STR_TRYCON;
                    break;
                case INT_DISCON:
                    text = STR_DISCON;
                    break;
                case INT_ERROR:
                    text = STR_ERROR;
                    break;
                case INT_CANCELED:
                    text = STR_CANCELED;
                    break;
                default:
                    text = STR_UNKNOWN;
            }
        } else if (i < 100) {
            b.setValue(i);
            return b;
        }
        
        super.getTableCellRendererComponent(table, text, isSelected, 
                                            hasFocus, row, column);
        return this;
    }
}